package com.study.homework;

public class ArrayQueue {

    private final int[] ints;
    private int head;
    private int tail;

    public ArrayQueue(int len) {
        ints = new int[len];
        head = -1;
        tail = -1;
    }

    public boolean push(int a) {
        if (head != -1 && head == tail) {
            return false;
        }
        head = (head + 1) % ints.length;
        ints[head] = a;
        return true;
    }

    public int pop() {
        int t = (tail + 1) % ints.length;
        if (t > head) {
            throw new RuntimeException("queue is empty");
        }
        int result = ints[t];
        tail = t;
        return result;
    }

    public int peek() {
        int t = (tail + 1) % ints.length;
        if (t > head) {
            throw new RuntimeException("queue is empty");
        }
        return ints[tail];
    }
}
